<?php

namespace app\apixcx\controller;

use app\admin\model\Card as CardModel;
use app\admin\model\User as UserModel;
use app\admin\model\Info as InfoModel;
use app\admin\model\Relation as RelationModel;

class Card
{
    protected $CardModel;
    protected $UserModel;
    protected $InfoModel;
    protected $RelationModel;

    public function __construct()
    {
        $this->CardModel = new CardModel();
        $this->UserModel = new UserModel();
        $this->InfoModel = new InfoModel();
        $this->RelationModel = new RelationModel();
    }

    /**
     * get 获取名片详细
     * cid 当前名片ID
     * uid 当前用户ID
     * type 来源类型.
     */
    public function get($cid, $uid, $type = 1)
    {
        $token = getheaders()['Http-Token'];
        if (!$token || $token != cache('token'.$uid)) {
            return -100;
        }
        $cardRes = $this->CardModel->get($cid);
        $infoRes = $this->InfoModel->where('gid', $cardRes->gid)->find();
        if ($cardRes) {
            //判断名片是否被关闭
            if (!$cardRes->is_disabled) {
                return -1000;
            }

            //更新浏览次数
            $this->CardModel->where('id', $cid)->setInc('view_num');

            //判断职位为普通职位隐藏手机号与微信号
            if (!$cardRes['job_id']['type']) {
                $cardRes['phone'] = '';
                $cardRes['wechat'] = '';
            }

            //创建浏览关系
            $RelationWhereData = [
                'uid' => $uid,
                'cid' => $cid,
                'gid' => $cardRes['gid'],
            ];
            $RelationSaveData = [
                'uid' => $uid,
                'cid' => $cid,
                'type' => $type,
                'gid' => $cardRes['gid'],
                'give_up' => 1,
            ];
            $RelationRes = $this->RelationModel->where($RelationWhereData)->find();
            if ($RelationRes) {
                if ($RelationRes['type'] = '-1') {
                    $RelationRes->type = $type;
                    $RelationRes->save();
                }
                $this->RelationModel->where($RelationWhereData)->setInc('view');
            } else {
                $RelationRes = $this->RelationModel->create($RelationSaveData);
            }
            $cardRes['relation_type'] = $RelationRes->type;
            $cardRes['give_up'] = $RelationRes->give_up;

            //查找绑定的名片人
            $userRes = $this->UserModel->where('cid', $cid)->field('id')->find();
            if ($userRes) {
                $cardRes['uid'] = $userRes['id'];
            } else {
                $cardRes['uid'] = 0;
            }

            //获取浏览人头像
            $viewsImg = [];
            $query = [
                'where' => [
                    'cid' => $cid,
                ],
                'order' => [
                    'create_time' => 'desc',
                ],
                'limit' => 10,
            ];
            $RelationImgRes = $this->RelationModel->getLst($query);
            foreach ($RelationImgRes as $k => $v) {
                if ($v['uid']) {
                    $viewsImg[] = $v['uid'];
                }
            }
            $cardRes['view_imgs'] = array_unique($viewsImg);

            //输出职位
            $cardRes['job_name'] = $cardRes['job_id']['name'];
            $cardRes['job_des'] = $cardRes['job_id']['des'];
            $cardRes['job_type'] = $cardRes['job_id']['type'];

            //输出公司信息
            $cardRes['company_logo'] = $infoRes['logo'];
            $cardRes['company_web'] = $infoRes['web'];
            if ($cardRes['company_id']) {
                $cardRes['company_title'] = $cardRes['company_id']['title'];
                $cardRes['company_adrress'] = $cardRes['company_id']['adrress'];
                $cardRes['company_tel'] = $cardRes['company_id']['tel'];
                $cardRes['company_lat'] = $cardRes['company_id']['lat'];
                $cardRes['company_lng'] = $cardRes['company_id']['lng'];
            } else {
                $cardRes['company_title'] = $infoRes['title'];
                $cardRes['company_adrress'] = $infoRes['adrress'];
                $cardRes['company_tel'] = $infoRes['tel'];
                $cardRes['company_lat'] = $infoRes['lat'];
                $cardRes['company_lng'] = $infoRes['lng'];
            }

            return $cardRes;
        } else {
            return 0;
        }
    }

    /**
     * giveUp 点赞名片
     * cid 当前名片ID
     * uid 当前用户ID.
     */
    public function giveUp($cid, $uid)
    {
        $token = getheaders()['Http-Token'];
        if (!$token || $token != cache('token'.$uid)) {
            return -100;
        }
        $data = [
            'uid' => $uid,
            'cid' => $cid,
        ];
        $RelationRes = $this->RelationModel->where($data)->find();
        $CardRes = $this->CardModel->get($cid);
        if ($RelationRes->give_up) {
            $this->RelationModel->where($data)->setDec('give_up');
            $this->CardModel->where('id', $cid)->setInc('give_num');
        }
        if ($RelationRes) {
            return 1;
        } else {
            return 0;
        }
    }
}
